//
//  CHExceptionlistInfoController.m
//  CarFile
//
//  Created by 陈浩 on 2017/7/18.
//  Copyright © 2017年 陈浩. All rights reserved.
//

#import "CHExceptionlistInfoController.h"
#import "CHNouploadCell.h"
#import "CHExceptionlistInfoModel.h"
#import "CHRepairDetailController.h"
#import "CHDemoViewController.h"
@interface CHExceptionlistInfoController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic ,strong) UITableView *tableView;
@property (nonatomic ,strong) NSMutableArray *datas;
@property (nonatomic ,assign) int page;
@end

@implementation CHExceptionlistInfoController
-(NSMutableArray *)datas{
    if (!_datas) {
        _datas = [NSMutableArray array];
    }
    return _datas;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"连续7天未上传企业";
    self.view.backgroundColor = KRGB(239, 239, 249);
    
    [self setTitleLabel];
    
    [self setupTableView];
 
}




-(void)setupTableView{
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 44, UIScreenW, UIScreenH - 64 -44)];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.backgroundColor = KRGB(239, 239, 249);
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  
    // 下拉刷新
    self.tableView.mj_header= [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        // 模拟延迟加载数据，因此2秒后才调用（真实开发中，可以移除这段gcd代码）
        [self getData];
        
    }];
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [self loadMoreData];
    }];
    self.tableView.mj_footer.automaticallyHidden = YES;
    [self.tableView.mj_header beginRefreshing];
    [self.view addSubview:self.tableView];
    
    
}

-(void)setTitleLabel{
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, UIScreenW - 30, 44)];
    titleLabel.font = [UIFont systemFontOfSize:14];
    titleLabel.numberOfLines = 0;
    titleLabel.text = [NSString stringWithFormat:@"%@有企业%@家，截止到目前，连续7天未上传企业有%@家。",self.model.areacn,self.model.total,self.model.exceptionnum];
    
    titleLabel.textColor = [UIColor colorWithHexString:@"999999" alpha:1.0f];
    [self.view addSubview:titleLabel];
    
    
}

//   返回前天的时间
- (NSString *)GetLastDay {
    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDateComponents *components = [gregorian components:NSCalendarUnitWeekday | NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:[NSDate date]];
    [components setDay:([components day]-1)];
    
    NSDate *beginningOfWeek = [gregorian dateFromComponents:components];
    NSDateFormatter *dateday = [[NSDateFormatter alloc] init];
    [dateday setDateFormat:@"yyyy-MM-dd"];
    return [dateday stringFromDate:beginningOfWeek];
}




-(void)loadMoreData{
    
    _page++;
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"areaid"] = self.model.areaid;
    params[@"usertype"] = [Util getValue:@"usertype"];
    params[@"user"] = [Globle getInstance].user;
    params[@"sign"] = [DESCript encrypt:self.model.areaid encryptOrDecrypt:kCCEncrypt key:[Globle getInstance].key];
    params[@"datetime"] = [self GetLastDay];
    params[@"pagesize"] = @"20";
    params[@"pagenum"] = [NSString stringWithFormat:@"%d",_page];
    [[Globle getInstance].service requestWithServiceName:@"lcipappgovexceptionlistinfo" params:params httpMethod:@"POST" resultIsDictionary:YES completeBlock:^(id result) {
        
        [self.tableView.mj_footer endRefreshing];
        if ([result[@"code"] isEqualToString:@"1"]) {
            for (NSDictionary *dict in result[@"data"][@"list"]) {
                CHExceptionlistInfoModel *model = [CHExceptionlistInfoModel objectWithKeyValues:dict];
                
                [self.datas addObject:model];
            }
            [self.tableView reloadData];
        }
    }];
    
}

-(void)getData{
    _page = 1;
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"areaid"] = self.model.areaid;
    params[@"usertype"] = [Util getValue:@"usertype"];
    params[@"user"] = [Globle getInstance].user;
    params[@"sign"] = [DESCript encrypt:self.model.areaid encryptOrDecrypt:kCCEncrypt key:[Globle getInstance].key];
    params[@"levels"] = self.levels;
    params[@"datetime"] = [self GetLastDay];
    params[@"pagesize"] = @"20";
    params[@"pagenum"] = [NSString stringWithFormat:@"%d",_page];
    [[Globle getInstance].service requestWithServiceName:@"lcipappgovexceptionlistinfo" params:params httpMethod:@"POST" resultIsDictionary:YES completeBlock:^(id result) {
        [self.datas removeAllObjects];
        [self.tableView.mj_header endRefreshing];
        NSLog(@"异常数据时间%@",result);
        if ([result[@"code"] isEqualToString:@"1"]) {
            for (NSDictionary *dict in result[@"data"][@"list"]) {
                CHExceptionlistInfoModel *model = [CHExceptionlistInfoModel objectWithKeyValues:dict];
             
                NSLog(@"%@",[model.nouploaddata[@"time"] class]);
                [self.datas addObject:model];
            }
          
        }
        self.tableView.placeholderImage = @"data_null";
        self.tableView.placeholder = @"暂时没有数据哦~";
        [self.tableView reloadData];
    }];
}



-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.datas.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    CHNouploadCell *cell = [CHNouploadCell cellForTableView:tableView];
     __weak typeof(self) weakSelf = self;
    cell.block = ^{
        
        
        CHDemoViewController *vc = [[CHDemoViewController alloc] init];
        CHExceptionlistInfoModel *model = weakSelf.datas[indexPath.row];
        
        vc.datas = model.nouploaddata[@"time"];
        [weakSelf.navigationController pushViewController:vc animated:YES];

    };
    cell.info_model = self.datas[indexPath.row];
    return cell;
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    id obj = [LongriseRouter openURL:@"CHRepairDetailController"];
    if(obj && [obj isKindOfClass:[UIViewController class]])
    {
        UIViewController *vc = obj;
        [self.navigationController pushViewController:vc animated:YES];
    }
    
    //判断该类是否可以传参数
    if([LongriseRouter isImplLongriseRouterDelegaterWithObject:obj])
    {
        id<LongriseRouterDelegater> delegater = obj;
        if(delegater)
        {
            
            CHExceptionlistInfoModel *model = self.datas[indexPath.row];
            NSMutableDictionary *params = [NSMutableDictionary dictionary];
            
            params[@"companyid"] = model.companyid;
            params[@"areaid"] = model.areaid;
            
            //进行传值，传入统一的block以供B进行回调
            [delegater setParams:params block:^(id obj) {
                
                
            }];
        }
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 150;
}

@end
